/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tcpserver;

/**
 *
 * @author makkalot
 */

import java.io.*;
import java.net.*;
import udpserver.SaveImage;


public abstract class GeneralTcpServer extends Thread{
    
    private ServerSocket simpleserver = null;
    private SaveImage savei = null;
	
    public static final int bindPort = 11111; //evt hardcoded var mi?
    public static final int MAX_IO_BUFF = 512;

    
    protected BufferedInputStream server_bi = null;
    protected DataInputStream server_di =null;
    protected Socket client_connection = null;
    protected DataOutputStream out = null;
    
    protected boolean cik_donguden = false; 
    
    public GeneralTcpServer() throws IOException{
    
        this.simpleserver = new ServerSocket(GeneralTcpServer.bindPort);
        
        System.out.println("The server is started on port :"+GeneralTcpServer.bindPort);
    }
    
    public void run(){
    
        //will run for a very long time indeed
        //we wont write data for now ... later maybe
        try{
        byte[] byte_seq = new byte[GeneralTcpServer.MAX_IO_BUFF]; //allocate for that
	
        System.out.println("Lan nerdesin sen ");
         while(true){
           
             System.out.println("Waiting someone to connects here ");
             client_connection = this.simpleserver.accept(); //wait for a conection here
           
           //this.server_bi = new BufferedInputStream(this.client_connection.getInputStream());
            server_di = new DataInputStream(client_connection.getInputStream());
            out = new DataOutputStream(client_connection.getOutputStream());
            
           System.out.println("I got a new connection !");
           while(server_di.read(byte_seq)!=-1){
           
          //burada icinde duruma gore islemlerini yapacansen ya
            
            //here tmp is the status code
            String tmp =this.handleData(byte_seq); //here we do saving data and etc.
           
            if(tmp.equals("start")){
                this.out.write("start".getBytes(), 0,"start".getBytes().length );
                this.out.flush();
                System.out.println("I sent client that i have started");
            }
            else if(tmp.equals("stop")){
                this.out.write("stop".getBytes(), 0,"stop".getBytes().length );
                System.out.println("I sent client that i have finished");
                this.out.flush();
            }
            
            byte_seq=null; //icinde bisiler kaliyo pust yakaladim seni
            byte_seq = new byte[GeneralTcpServer.MAX_IO_BUFF];
           //if there is no data to send
            //this.server_di.reset();
           }
            
            try{
                
            client_connection.close();
            client_connection = null;
            server_di.close();
            server_di = null;
            System.out.println("Oclosed my connections here !");
            }
            catch(IOException e){
                System.out.println("Korhma bisi olmaz lan");
            
            }
         }
        
        }
        catch(IOException e){
           System.out.println("blip blip error in server");
            
        }
    }
    
    //should be implemented from other classes
    public abstract String handleData(byte[] data);
}
